#include "BossLvl1Strategy.h"
#include "Weapon.h"
#include "Bullet.h"
#include "PlayerPlane.h"
#include "MainLayer.h"
#include "Config.h"
#include "MathUtil.h"
#include "WeaponBank.h"
#include "CNumber.h"
#include "StrategyBank.h"

BossLvl1Strategy::BossLvl1Strategy(void)
{
	_weaponShootPlaneLeft = NULL;
	_beamShootStrategy = NULL;
	_shootPosition = NULL;
	_timeShootPlaneDelay = 0;
	_timeDelayStart = 0;
}


BossLvl1Strategy::~BossLvl1Strategy(void)
{
	CC_SAFE_RELEASE(_weaponShootPlaneLeft);
	CC_SAFE_RELEASE(_shootPosition);
	CC_SAFE_RELEASE(_beamShootStrategy);
}

bool BossLvl1Strategy::init()
{
	if (!BaseStrategy::init())
		return false;
	setShootPositions(CCArray::create());
	return true;
}

void BossLvl1Strategy::processing( float dt )
{
	// Move process
}

void BossLvl1Strategy::parseXml( xml_node node )
{
	char* weapon1 = (char*)node.child("Weapon1").text().as_string();
	Weapon* wp1 = WeaponBank::sharedWeaponBank()->getWeapon(weapon1);
	setWeaponShootPlaneLeft(wp1);

	setTimeShootPlaneDelay(node.child("TimeDelay1").text().as_float());
	setTimeDelayStart(node.child("TimeDelayStart").text().as_float());

	int x = node.child("ShootTargetPosition1").child("X").text().as_int();
	int y = node.child("ShootTargetPosition1").child("Y").text().as_int();
	_shootPosition->addObject(CCNumber<CCPoint>::create(ccp(x, y)));

	x = node.child("ShootTargetPosition2").child("X").text().as_int();
	y = node.child("ShootTargetPosition2").child("Y").text().as_int();
	_shootPosition->addObject(CCNumber<CCPoint>::create(ccp(x, y)));

	// For Beam strategy
	xml_node beamStrategy = node.child("AiBeam");
	char* cStrategy = (char*)beamStrategy.child("Id").text().as_string();
	BaseStrategy* strategy = StrategyBank::sharedStrategyBank()->createStrategy(cStrategy);
	strategy->parseXml(node.child("Data"));
	setBeamShootStrategy(strategy);
}

void BossLvl1Strategy::onEnter()
{
	BaseStrategy::onEnter();
	CCMoveTo* moveTo = CCMoveTo::create(2, ccp(200, 700));
	getOwner()->runAction(moveTo);

	runAction(CCSequence::create(CCDelayTime::create(_timeDelayStart), CCCallFuncN::create(this, SEL_CallFuncN(&BossLvl1Strategy::startStrategy)), NULL));
}

void BossLvl1Strategy::onExit()
{
	BaseStrategy::onExit();
}

Weapon* BossLvl1Strategy::getWeaponShootPlaneLeft()
{
	return _weaponShootPlaneLeft;
}

void BossLvl1Strategy::setWeaponShootPlaneLeft(Weapon* var)
{
	if (_weaponShootPlaneLeft != var)
	{
		removeChild(_weaponShootPlaneLeft);
		CC_SAFE_RETAIN(var);
		CC_SAFE_RELEASE(_weaponShootPlaneLeft);
		_weaponShootPlaneLeft = var;
		addChild(_weaponShootPlaneLeft);
	}
}

void BossLvl1Strategy::onShootPlane( float dt )
{
	// Calculate shoot target to plane on 2 side
	CCArray* bullets1 = _weaponShootPlaneLeft->addBullet();
	CCArray* bullets2 = _weaponShootPlaneLeft->addBullet();

	PlayerPlane* plane = getOwner()->getMainLayer()->getMyPlant();
	//
	{
		// For calculate direct shoot angle
		Bullet* bu = dynamic_cast<Bullet*>(bullets1->objectAtIndex(0));
		CCPoint buPos = bu->getPosition();
		CCPoint shootPos = dynamic_cast<CCNumber<CCPoint>*>(_shootPosition->objectAtIndex(0))->getNumber() + getOwner()->getPosition() + buPos;
		bu->setPosition(shootPos);

		CCPoint directToPlayer = ccp(plane->boundingBox().getMidX() - bu->boundingBox().getMidX(),
			plane->boundingBox().getMidY() - bu->boundingBox().getMidY());
		float angle = MathUtil::angle2Vetor(VECTOR_MAIN, directToPlayer);
		
		bu->setVelocity(MathUtil::rotateVector(bu->getVelocity(), angle));
		CCRect buRect = bu->boundingBox();
		// Change all direct angle shoot
		for (int i = 1; i < bullets1->count(); i++)
		{
			Bullet* bullet = dynamic_cast<Bullet*>(bullets1->objectAtIndex(i));
			CCPoint bu1Pos = bullet->getPosition();
			bullet->setPosition(dynamic_cast<CCNumber<CCPoint>*>(_shootPosition->objectAtIndex(0))->getNumber() + getOwner()->getPosition() + bu1Pos);
			bullet->setVelocity(MathUtil::rotateVector(bullet->getVelocity(), angle));
		}
	}
	//
	
	{
		Bullet* bu = dynamic_cast<Bullet*>(bullets2->objectAtIndex(0));
		CCPoint buPos = bu->getPosition();
		CCPoint shootPos = dynamic_cast<CCNumber<CCPoint>*>(_shootPosition->objectAtIndex(1))->getNumber() + getOwner()->getPosition() + buPos;
		bu->setPosition(shootPos);

		CCPoint directToPlayer = ccp(plane->boundingBox().getMidX() - bu->boundingBox().getMidX(),
			plane->boundingBox().getMidY() - bu->boundingBox().getMidY());
		float angle = MathUtil::angle2Vetor(VECTOR_MAIN, directToPlayer);

		bu->setVelocity(MathUtil::rotateVector(bu->getVelocity(), angle));
		CCRect buRect = bu->boundingBox();
		// Change all direct angle shoot
		for (int i = 1; i < bullets2->count(); i++)
		{
			Bullet* bullet = dynamic_cast<Bullet*>(bullets2->objectAtIndex(i));
			CCPoint bu1Pos = bullet->getPosition();
			bullet->setPosition(dynamic_cast<CCNumber<CCPoint>*>(_shootPosition->objectAtIndex(1))->getNumber() + getOwner()->getPosition() + bu1Pos);
			bullet->setVelocity(MathUtil::rotateVector(bullet->getVelocity(), angle));
		}
	}
}

void BossLvl1Strategy::startStrategy( CCNode* sender )
{
	onShootPlane(_timeShootPlaneDelay);
	schedule(SEL_SCHEDULE(&BossLvl1Strategy::onShootPlane), _timeShootPlaneDelay);
}

